/*
 * @lc app=leetcode.cn id=1365 lang=javascript
 *
 * [1365] 有多少小于当前数字的数字
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var smallerNumbersThanCurrent = function(nums) {
    var nums_copy = JSON.parse(JSON.stringify(nums));
    for(let i=0;i<nums_copy.length-1;i++){
        for(let j=i+1;j<nums_copy.length;j++){
            if(nums_copy[j]<nums_copy[i]){
                let temp = nums_copy[i];
                nums_copy[i] = nums_copy[j];
                nums_copy[j] =  temp;
            }
        }
    }
   
    var result = new Array(nums.length).fill(0);
    for(let i=0;i<nums.length;i++){
        let left = 0, right = nums.length-1, middle = 0;
        while(left<=right){
            middle = Math.floor((left+right)/2);
            if(nums_copy[middle]>nums[i]){
                right = middle-1;
            } else if(nums_copy[middle]<nums[i]){
                left = middle+1;
            } else {
                while(nums_copy[middle]===nums[i]){
                    middle--;
                }
                result[i] = middle+1;
                break;
            }
        }
    }
    return result;
};
console.time('s');
smallerNumbersThanCurrent([9,4,8,3,7,1,0,2]);
console.timeEnd('s');
// if(!undefined) console.log(true);




// @lc code=end

